package cn.yitun.chat.yitunservercommonlib.databind.jackson;

import cn.yitun.chat.yituncommonutil.YitunDateUtil;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * 
 * Description 时间格式反序列化 <br>
 * Date 2019-04-29 09:26:46<br>
 * 
 * @author yitun
 * @since 1.1
 */
public class CustomLocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {

	@Override
	public LocalDateTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
		LocalDateTime localDateTime = null;
		String text = jsonParser.getText();
		if (null != text) {
			String date = YitunDateUtil.allToDateTimeMillisecond(text);
			DateTimeFormatter formatter = DateTimeFormatter.ofPattern(YitunDateUtil.FORMAT_DATE_TIME_MILLISECOND);
			localDateTime = LocalDateTime.parse(date, formatter);
		}
		return localDateTime;
	}
}
